//
//  CJRewardVideoAdapter.m
//  CJMobileAdapter
//
//  Created by ly on 2024/3/1.
//

#import "CJRewardVideoAdapter.h"
#import "CJAdapterHeader.h"
#import "CJRewardVideoCustomEvent.h"
#import "CJATTMBiddingManager.h"
#import "CJAdapterSetup.h"

@interface CJRewardVideoAdapter()

@property (nonatomic, strong) CJRewardVideoCustomEvent *customEvent;
@property (nonatomic, strong) CJRewardVideoAd *rewardVideoAd;

@end
@implementation CJRewardVideoAdapter

-(instancetype) initWithNetworkCustomInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo {
    self = [super init];
    if (self = [super init]) {
//        [CJAdapterSetup setupCJSDKWithAppId:@"appid"];
    }
    return self;
}

-(void)loadADWithInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo completion:(void (^)(NSArray<NSDictionary *> *, NSError *))completion {
    _customEvent = [[CJRewardVideoCustomEvent alloc] initWithInfo:serverInfo localInfo:localInfo];
    _customEvent.requestCompletionBlock = completion;
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:serverInfo[@"unitid"]];
    if (request) {
        if (request.customObject != nil) { // load secced
            self.rewardVideoAd = request.customObject;
            self.rewardVideoAd.delegate = _customEvent;
            [_customEvent trackRewardedVideoAdLoaded:self.rewardVideoAd adExtra:@{}];
        
        } else { // fail
            NSError *error = [NSError errorWithDomain:ATADLoadingErrorDomain code:ATAdErrorCodeThirdPartySDKNotImportedProperly userInfo:@{NSLocalizedDescriptionKey:@"AT has failed to load splash.", NSLocalizedFailureReasonErrorKey:@"It took too long to load placement stragety."}];
            [_customEvent trackRewardedVideoAdLoadFailed:error];
        }
        [[CJATTMBiddingManager sharedInstance] removeRequestItmeWithUnitID:serverInfo[@"unitid"]];
    } else {
        NSDictionary *extra = localInfo;
        self.rewardVideoAd = [[CJRewardVideoAd alloc] initWithResourceId:serverInfo[@"unitid"] userId:extra[kATAdLoadingExtraUserIDKey] extend:extra[kATAdLoadingExtraMediaExtraKey]];
        self.rewardVideoAd.delegate = self.customEvent;
        [self.rewardVideoAd loadAdData];
    }
}

+ (BOOL)adReadyWithCustomObject:(id)customObject info:(NSDictionary*)info {
    return ((CJRewardVideoAd *)customObject) ? true : false;
}

+ (void)showRewardedVideo:(ATRewardedVideo*)rewardedVideo inViewController:(UIViewController*)viewController delegate:(id<ATRewardedVideoDelegate>)delegate {
    CJRewardVideoCustomEvent *customEvent = (CJRewardVideoCustomEvent*)rewardedVideo.customEvent;
    customEvent.delegate = delegate;
    [((CJRewardVideoAd *)rewardedVideo.customObject) showFromRootViewController:viewController];
}

+ (void)bidRequestWithPlacementModel:(ATPlacementModel *)placementModel unitGroupModel:(ATUnitGroupModel *)unitGroupModel info:(NSDictionary *)info completion:(void(^)(ATBidInfo *bidInfo, NSError *error))completion {
    if (NSClassFromString(@"CJADManager") == nil) {
        if (completion != nil) { completion( nil, [NSError errorWithDomain:@"com.ubix.mediation.ios" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Bid request has failed", NSLocalizedFailureReasonErrorKey:@"CJAD is not imported"}]); }
        return;
    }
    
    if (NSClassFromString(@"CJADManager") == nil) {
        if (completion != nil) { completion( nil, [NSError errorWithDomain:@"com.ubix.mediation.ios" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Bid request has failed", NSLocalizedFailureReasonErrorKey:@"TianmuSDK is not imported"}]); }
        return;
    }
    [CJADManager configure:info[@"appid"]];
    CJATTMBiddingManager *biddingManage = [CJATTMBiddingManager sharedInstance];
    CJATTMBiddingRequest *request = [CJATTMBiddingRequest new];
    request.unitGroup = unitGroupModel;
    request.placementID = placementModel.placementID;
    request.bidCompletion = completion;
    request.unitID = info[@"unitid"];
    request.extraInfo = info;
    request.adType = ESCAdFormatRewardVideo;
    dispatch_async(dispatch_get_main_queue(), ^{
        CJRewardVideoAd *rewardVideoAd = [[CJRewardVideoAd alloc] initWithResourceId:info[@"unitid"] userId:@"" extend:@""];
        request.customObject = rewardVideoAd;
        [biddingManage startWithRequestItem:request];
        [rewardVideoAd loadAdData];
    });
}

+ (void)sendWinnerNotifyWithCustomObject:(id)customObject secondPrice:(NSString*)price userInfo:(NSDictionary<NSString *, NSString *> *)userInfo {
    CJSplashAd *splashAd = (CJSplashAd *)customObject;
    NSLog(@"比价成功");
}

+ (void)sendLossNotifyWithCustomObject:(nonnull id)customObject lossType:(ATBiddingLossType)lossType winPrice:(nonnull NSString *)price userInfo:(NSDictionary *)userInfo {
    NSLog(@"比价失败");
}

@end
